﻿using System;
using CompactMapper.Orm.Annotations;

namespace CompactMapper.Orm.UnitOfWork.Mappings.Meta.PropertyInfos.Columns
{
    public class TextColumn : Column
    {
        private readonly String _sqlType;

        public TextColumn(PropertyInfoPersistenceAttributePair pair)
            : base(pair.PropertyInfo)
        {
            var textColumnAttribute = (TextColumnAttribute)pair.PersistenceAttribute;
            _sqlType = String.Format("[nvarchar]({0})", textColumnAttribute.MaxLength);
        }


        public override string SqlType
        {
            get { return _sqlType; }
        }


        protected override object ToSqLiteObjectNonEnumeration(object target)
        {
            var str = GetValue(target);
            if (str != null)
            {
                return str.ToString();
            }
            return String.Empty;
        }

        protected override object FromSqLiteObjectNonEnumeration(object source)
        {
            if (source is DBNull)
            {
                return null;
            }
            return source;
        }
    }
}